#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: logger_service.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

The logger service uses the standard Python logging module to set up file
and console logging.

"""

#-----------------------------------------------------------------------
# System imports
import sys
import logging

# Application imports
import config_service as config

#-----------------------------------------------------------------------
# Initialise the logging system. This uses the standard python logging service.
def init_logger():
    # Set up the logging service
    filename = config.log_file()
    try:
        if(filename != None):
            # Set up simple file based logger
            logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename=filename,
                    filemode='a')
        else:
            print 'There is no logger filename in the configuration.'
            print 'Quiet Mode will be over-ridden'
            print 'Console logging only will be provided'
        if not config.quiet() or filename == None:
            # Define a Handler which writes INFO messages or higher to the sys.stderr
            console = logging.StreamHandler()
            console.setLevel(logging.INFO)
            # Set a format which is simpler for console use
            formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
            # Tell the handler to use this format
            console.setFormatter(formatter)
            # Add the handler to the root logger
            logging.getLogger('').addHandler(console)
            logger = logging.getLogger('pylink.logger')
    except:
        print 'There is a problem creating the logging system, unable to continue [' + str(sys.exc_info()) + ']'
        sys.exit(1)